//
//  CLevel.m
//  iSudoku
//
//  Created by acumen on 5/23/13.
//  Copyright (c) 2013 Tri Vo. All rights reserved.
//

#import "CLevel.h"
#import "GameUtils.h"

static CLevel *instance = nil;

@implementation CLevel

+ (id)getInstance {
    if (!instance) {
        instance = [[CLevel alloc] init];
    }
    return instance;
}

- (id)init {
    self = [super init];
    if (self) {
        mEmptyMatrix = [[CRL2DArray alloc] initWithRows:9 columns:9];
        [self fillEmpty];
    }
    return self;
}

- (void)dealloc {
    [mEmptyMatrix release];
    [super dealloc];
}


#pragma mark - Class Methods

- (void) fillEmpty {
    int qCells;
    switch ([[GameUtils getInstance] getCurrentLevel]) {
        case GAME_LEVEL_EASY:
        {
            // 20 ~ 30 empty cells
            qCells = arc4random()*10 + 20;
        }
            break;
        case GAME_LEVEL_NORMAL:
        {
            // 30 ~ 45 empty cells
            qCells = arc4random()*15 + 30;
        }
            break;
        case GAME_LEVEL_HARD:
        {
            // 50 ~ 65 empty cells
            qCells = arc4random()*15 + 50;
        }
            break;
        default:
        {
            qCells = 0;
        }
            break;
    }
    [self setEmptyCells:qCells];
}

- (void) setEmptyCells:(int)quantityOfEmptyCell {
    for (int i = 0; i < quantityOfEmptyCell; i++) {
        // check whether cell already selected
        BOOL isDuplicate = YES;
        do {
            int x = arc4random()*9;
            int y = arc4random()*9;
            NSNumber *tempNumber = [mEmptyMatrix objectAtRow:x column:y withTypeClass:[NSNumber class]];
            if ([tempNumber intValue] == 0) {
                isDuplicate = YES;
            } else {
                [mEmptyMatrix insertObject:[NSNumber numberWithInt:0] atRow:x column:y];
                isDuplicate = NO;
            }
        } while (isDuplicate);
    }
}

- (CRL2DArray *)getCurrentMatrix {
    return mEmptyMatrix;
}


@end
